package tdh.xt.entity;

import tdh.xt.common.utils.UrlUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * @package com.xt.util
 * @description 为了更方便地将所有get请求中的参数都解码，自定义了一个请求类
 * @author Ricardo
 * @date 2021/5/10 2:41
 * @copyright (c) 2021,南京通达海科技股份有限公司 All Rights Reserved.
 * @version 1.0
 * Modification History:
 * Date                  Author      Version     Description
 * ------------------------------------------------------------------
 * 2021/5/10 2:41       Ricardo       1.0    为了更方便地将所有get请求中的参数都解码，自定义了一个请求类
 */
public class MyRequest extends HttpServletRequestWrapper {
    /**
     * @param request request
     * @description 默认构造函数
     * @author RicarAN
     * @date 2021/5/10 2:42
     * @version 1.0
     */
    public MyRequest(HttpServletRequest request) {
        super(request);
    }

    /**
     * @param name 参数名
     * @return java.lang.String
     * @description 获取参数
     * @author RicarAN
     * @date 2021/5/10 2:42
     * @version 1.0
     */
    @Override
    public String getParameter(String name) {
        //进行请求编码处理
        HttpServletRequest request = (HttpServletRequest) getRequest();
        String value = request.getParameter(name);
        //用户请求方式
        if ("GET".equals(request.getMethod())) {
            //获取调用getParameter(name)的值
            if (value == null) {
                return null;
            }
            return UrlUtil.URLDecode(value);
        }
        return value;
    }

    /**
     * @param name 参数名
     * @return java.lang.String[]
     * @description 获取参数的数组值
     * @author RicarAN
     * @date 2021/5/10 2:42
     * @version 1.0
     */
    @Override
    public String[] getParameterValues(String name) {
        HttpServletRequest request = (HttpServletRequest) getRequest();
        String[] values = getRequest().getParameterValues(name);
        if ("GET".equals(request.getMethod())) {
            if (values == null) {
                return null;
            }
            for (int i = 0; i < values.length; i++) {
                values[i] = UrlUtil.URLDecode(values[i]);
            }
            //返回编码后的数据
            return values;
        }
        return values;
    }
}
